package com.cf.r1042_ak;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

public class f {

    static class Pair {
        long val;
        long By;
        long y;
        Pair(long val, long By, long y) { this.val = val; this.By = By; this.y = y; }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        int t = Integer.parseInt(br.readLine());
        while (t-- > 0) {
            int n = Integer.parseInt(br.readLine());
            char[] a = br.readLine().toCharArray();
            char[] b = br.readLine().toCharArray();
            long[] prefA = new long[n+1];
            long[] prefB = new long[n+1];
            for (int i = 1; i <= n; i++) {
                prefA[i] = prefA[i-1] + (a[i - 1] == '1' ? 1 : 0);
                prefB[i] = prefB[i-1] + (b[i - 1] == '1' ? 1 : 0);
            }
            Pair[] arr = new Pair[n];
            for (int y = 1; y <= n; y++) {
                long Ty = prefB[y];
                long val = 2*Ty - y;
                arr[y-1] = new Pair(val, Ty, y);
            }
            Arrays.sort(arr, Comparator.comparingLong(p -> p.val));

            long[] prefBySorted = new long[n+1];
            long[] prefYSorted = new long[n+1];
            for (int i = 0; i < n; i++) {
                prefBySorted[i + 1] = prefBySorted[i] + arr[i].By;
                prefYSorted[i + 1] = prefYSorted[i] + arr[i].y;
            }
            long totalBy = prefBySorted[n];
            long totalY = prefYSorted[n];

            long answer = 0L;
            for (int x = 1; x <= n; x++) {
                long A = prefA[x];
                long T = x - 2*A;
                int k = upperBoundVal(arr, T);
                long sum1 = (long)k * A + prefBySorted[k];
                long remCount = n - k;
                long sumY_rem = totalY - prefYSorted[k];
                long sumB_rem = totalBy - prefBySorted[k];
                long sum2 = remCount * (x - A) + (sumY_rem - sumB_rem);
                answer += sum1 + sum2;
            }

            out.println(answer);
        }
        out.flush();
        out.close();
        br.close();
    }

    static int upperBoundVal(Pair[] arr, long T) {
        int l = 0, r = arr.length;
        while (l < r) {
            int m = (l + r) >>> 1;
            if (arr[m].val <= T) l = m + 1;
            else r = m;
        }
        return l;
    }

}